<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/23 0023
 * Time: 21:05
 */
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

$client->set(array(
    'open_length_check' => 1,
    'package_length_type' => 'N',
    'package_length_offset' => 0,       //第N个字节是包长度的值
    'package_body_offset' => 4,       //第几个字节开始计算长度
    'package_max_length' => 2000000,  //协议最大长度
));
//注册连接成功回调
$client->on("connect", function ($cli) {
    $str = '123456789';
    $data = pack('N', strlen($str)) . $str;
    $cli->send($data);
});

//注册数据接收回调
$client->on("receive", function ($cli, $data) {
    echo "Received: " . $data . "\n";
});

//注册连接失败回调
$client->on("error", function ($cli) {
    echo "Connect failed\n";
});

//注册连接关闭回调
$client->on("close", function ($cli) {
    echo "Connection close\n";
});

//发起连接
$client->connect('127.0.0.1', 9502, 0.5);
